#include "studentmanager.h"
#include <iostream>
#include <cctype>
#include <iomanip>
#include <limits>

// 查找学生
int StudentManager::findStudentById(int id) const {
    for (int i = 0; i < count; i++) {
        if (students[i].getSid() == id) {
            return i;
        }
    }
    return -1;
}

// 菜单系统
void StudentManager::displayMenu() const {
    std::cout << "\nWelcome to student management system\n";
    std::cout << "1. Add student\n";
    std::cout << "2. View student\n";
    std::cout << "3. Delete student\n";
    std::cout << "4. Update student\n";
    std::cout << "5. Exit\n";
    std::cout << "Enter your choice: ";
}

void StudentManager::runMenu() {
    int choice;
    
    do {
        displayMenu();
        std::cin >> choice;
        
        // 清除输入缓冲区
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        
        switch(choice) {
            case 1:
                add_student();
                break;
            case 2:
                view_students();
                break;
            case 3:
                delete_student();
                break;
            case 4:
                update_student();
                break;
            case 5:
                std::cout << "Exiting system...\n";
                break;
            default:
                std::cout << "Invalid choice! Please enter 1-5.\n";
                std::cin.clear(); // 清除错误状态
        }
    } while(choice != 5);
}

// 增加学生函数
void StudentManager::add_student() {
    if (count >= MAX_STUDENTS) {
        std::cout << "Error: Maximum student capacity reached (" 
                  << MAX_STUDENTS << " students).\n";
        return;
    }

    Student newStudent;
    int id;
    
    // 输入学生ID
    std::cout << "Enter student ID (1-1000): ";
    if (!(std::cin >> id)) {
        std::cout << "Invalid input for ID.\n";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        return;
    }
    
    // 验证ID唯一性
    if (findStudentById(id) != -1) {
        std::cout << "Error: ID " << id << " already exists.\n";
        return;
    }
    
    // 设置学生ID
    if (!newStudent.setSid(id)) {
        std::cout << "Invalid ID. Must be between 1-1000.\n";
        return;
    }
    
    // 输入学生姓名
    char name[50];
    std::cout << "Enter student name: ";
    std::cin.ignore(); // 清除之前输入遗留的换行符
    std::cin.getline(name, 50);
    
    if (!newStudent.setSname(name)) {
        std::cout << "Error: Name cannot be empty.\n";
        return;
    }
    
    // 输入学生年龄
    int age;
    std::cout << "Enter student age (1-120): ";
    if (!(std::cin >> age)) {
        std::cout << "Invalid input for age.\n";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        return;
    }
    
    if (!newStudent.setSage(age)) {
        std::cout << "Invalid age. Must be between 1-120.\n";
        return;
    }
    
    // 输入学生性别
    char sex;
    std::cout << "Enter student gender (M/F): ";
    std::cin >> sex;
    
    if (!newStudent.setSsex(sex)) {
        std::cout << "Invalid gender. Must be 'M' or 'F'.\n";
        return;
    }
    
    // 添加新学生
    students[count++] = newStudent;
    std::cout << "Student added successfully!\n";
}

// 查看学生函数
void StudentManager::view_students() {
    if (count == 0) {
        std::cout << "No students in the system.\n";
        return;
    }
    
    // 表格头部
    std::cout << "\n" << std::setw(8) << std::left << "ID"
              << std::setw(20) << std::left << "Name"
              << std::setw(8) << std::left << "Age"
              << std::setw(8) << std::left << "Gender" << "\n";
    std::cout << std::setfill('-') << std::setw(50) << "" << std::setfill(' ') << "\n";
    
    // 表格数据
    for (int i = 0; i < count; i++) {
        std::cout << std::setw(8) << std::left << students[i].getSid()
                  << std::setw(20) << std::left << students[i].getSname()
                  << std::setw(8) << std::left << students[i].getSage()
                  << std::setw(8) << std::left << students[i].getSsex() << "\n";
    }
}

// 删除学生函数
void StudentManager::delete_student() {
    if (count == 0) {
        std::cout << "No students in the system.\n";
        return;
    }
    
    int id;
    std::cout << "Enter student ID to delete: ";
    if (!(std::cin >> id)) {
        std::cout << "Invalid input for ID.\n";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        return;
    }
    
    int index = findStudentById(id);
    if (index == -1) {
        std::cout << "Error: Student with ID " << id << " not found.\n";
        return;
    }
    
    // 删除学生 (将最后一位移到删除位置)
    students[index] = students[count - 1];
    count--;
    
    std::cout << "Student deleted successfully!\n";
}

// 更新学生函数
void StudentManager::update_student() {
    if (count == 0) {
        std::cout << "No students in the system.\n";
        return;
    }
    
    int id;
    std::cout << "Enter student ID to update: ";
    if (!(std::cin >> id)) {
        std::cout << "Invalid input for ID.\n";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        return;
    }
    
    int index = findStudentById(id);
    if (index == -1) {
        std::cout << "Error: Student with ID " << id << " not found.\n";
        return;
    }
    
    Student& student = students[index];
    char choice;
    
    // 更新姓名
    std::cout << "Current name: " << student.getSname() << "\n";
    std::cout << "Update name? (y/n): ";
    std::cin >> choice;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    
    if (std::tolower(choice) == 'y') {
        char newName[50];
        std::cout << "Enter new name: ";
        std::cin.getline(newName, 50);
        if (!student.setSname(newName)) {
            std::cout << "Error: Name update failed.\n";
        }
    }
    
    // 更新年龄
    std::cout << "Current age: " << student.getSage() << "\n";
    std::cout << "Update age? (y/n): ";
    std::cin >> choice;
    
    if (std::tolower(choice) == 'y') {
        int newAge;
        std::cout << "Enter new age (1-120): ";
        if (std::cin >> newAge) {
            if (!student.setSage(newAge)) {
                std::cout << "Invalid age. Must be between 1-120.\n";
            }
        } else {
            std::cout << "Invalid input for age.\n";
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
    }
    
    // 更新性别
    std::cout << "Current gender: " << student.getSsex() << "\n";
    std::cout << "Update gender? (y/n): ";
    std::cin >> choice;
    
    if (std::tolower(choice) == 'y') {
        char newSex;
        std::cout << "Enter new gender (M/F): ";
        std::cin >> newSex;
        
        if (!student.setSsex(newSex)) {
            std::cout << "Invalid gender. Must be 'M' or 'F'.\n";
        }
    }
    
    std::cout << "Student updated successfully!\n";
}